package logic

import (
	"gorm.io/gorm"
	"project/study-go/db"
	"project/study-go/ecode"
	"project/study-go/model"
)

//TopicLogic 改类型为方便划分logic包下的不同逻辑
type TopicLogic struct{}

var DefaultTopic = TopicLogic{}

//创建 topicnode
func (t TopicLogic) CreateTopicNode(node *model.TopicNode) ecode.ErrorCode {
	//创建主题节点
	ret := db.MasterDB.Create(node)
	if ret.Error != nil {
		return ecode.ErrorCode{ecode.ErrInternalError, 500}
	}
	return ecode.ErrorCode{nil, 200}
}

//ListTopicNode 列出所有帖子节点（板块）
func (t TopicLogic) ListTopicNode() ([]*model.TopicNode, ecode.ErrorCode) {
	var topicNodes []*model.TopicNode
	result := db.MasterDB.Find(&topicNodes)
	if result.Error != nil && result.Error != gorm.ErrRecordNotFound {
		return nil, ecode.ErrorCode{ecode.ErrInternalError, 500}
	}

	return topicNodes, ecode.ErrorCode{nil, 200}
}

//列出某主题下的所有帖子
func (t TopicLogic) ListTopics(params map[string]interface{}) (int64, []*model.Topic, ecode.ErrorCode) {
	var topics []*model.Topic
	var count int64
	var tx *gorm.DB = db.MasterDB

	if pagesize, ok := params["page_size"].(int); ok {
		tx = tx.Limit(pagesize)
		if page, ok := params["page"].(int); ok {
			tx = tx.Offset((page - 1) * pagesize)
		}
	}
	if orderby, ok := params["order_by"].(string); ok {
		tx = tx.Order(orderby)
	}
	if tid, ok := params["nid"].(string); ok {
		tx = tx.Where("nid = ?", tid)
	}

	sess := tx.Where("deleted = ?", 0).Session(&gorm.Session{})

	ret := sess.Table("topic").Joins("left join user_registry as u on u.uid = topic.uid").Count(&count)
	if ret.Error != nil {
		return count, nil, ecode.ErrorCode{ecode.ErrInternalError, 500}
	}
	ret = sess.Select("topic.*, u.username AS create_by").Joins("left join user_registry u ON u.uid = topic.uid").Find(&topics)
	if ret.Error != nil && ret.Error != gorm.ErrRecordNotFound {
		return count, nil, ecode.ErrorCode{ecode.ErrInternalError, 500}
	}
	return count, topics, ecode.ErrorCode{nil, 200}
}

//TopicNodeNameExist 主题节点名是否已存在
func (t TopicLogic) TopicNodeNameExist(name string) ecode.ErrorCode {
	node := &model.TopicNode{}
	ret := db.MasterDB.Select("name").Where("name = ?", name).First(node)
	if ret.Error == gorm.ErrRecordNotFound {
		return ecode.ErrorCode{nil, 200}
	} else if ret.Error != nil {
		return ecode.ErrorCode{ecode.ErrInternalError, 500}
	}
	return ecode.ErrorCode{ecode.ErrTopicNodeNameExist, 400}
}

//CreateTopic 创建一篇帖子
func (t TopicLogic) CreateTopic(topic *model.Topic) ecode.ErrorCode {
	ret := db.MasterDB.Create(topic)
	if ret.Error != nil {
		return ecode.ErrorCode{ecode.ErrInternalError, 500}
	}
	return ecode.ErrorCode{nil, 200}
}

//GetTopic 获取一篇帖子
func (t TopicLogic) GetTopic(tid string) (*model.Topic, ecode.ErrorCode) {
	topic := &model.Topic{}
	ret := db.MasterDB.Select("topic.*, u.username as create_by").Joins("left join user_registry  u on u.uid = topic.uid").Where("tid = ?", tid).Take(topic)
	if ret.Error == gorm.ErrRecordNotFound {
		return nil, ecode.ErrorCode{ecode.ErrTopicNotFound, 400}
	} else if ret.Error != nil {
		return nil, ecode.ErrorCode{ecode.ErrInternalError, 500}
	}

	return topic, ecode.ErrorCode{nil, 200}
}
